package homework2;

import java.io.*;
import java.util.ArrayList;

/**
 * LOC()
 * 
 * @data 2013.7.9
 * @declaration read data, and calculate the line of code
 * @author 121250185
 * 
 * Listing Contents:
 * @method: main(String[] args)
 * 			,readFile(String path)
 * 			,readMethodLine(String path)
 */

public class LOC {

	static ArrayList<String> method = new ArrayList<String>();;
	
	public static final String PATH = "Final.java";
	
	public static void main(String[] args) {
		int loc1 = readFile("./src/homework8/" + PATH);
		System.out.println("The line of code in " + PATH +" (without blank line and comment) is: "+loc1);
		readMethodLine("./src/homework8/" + PATH);
		System.out.println(PATH + " have " + method.size()/2 + " method.");
		for (int i = 0; i < method.size()/2; i++) {
			System.out.println("Method " + method.get(i) + " have " + method.get(i+method.size()/2) +" lines");
		}

	}// end

	/**
	 * readFile(String path)
	 * 
	 * @declaration: read the data from file
	 * @param path
	 */
	public static int readFile(String path) {
		int line = 0;
		String temp = "";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(path));
			while ((temp = br.readLine()) != null) {
				if(!(temp.trim().startsWith("/") || temp.trim().startsWith("*") ||  temp.trim().equals(""))){
					line++;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return line;
	}// end
	
	/**
	 * readMethodLine(String path)
	 * 
	 * @declaration: read the method name and line from file
	 * @param path
	 * 
	 */
	public static void readMethodLine(String path) {
		int i = 0;
		int line = 0;
		String temp = "";
		BufferedReader br = null;
		boolean flag = false;
		try {
			br = new BufferedReader(new FileReader(path));
			while ((temp = br.readLine()) != null) {
				if (flag) {
					if (temp.contains(method.get(i)) && !temp.trim().startsWith("*")) {
						line++;
						while (!((temp = br.readLine()).trim().startsWith("}") && temp.contains("// end"))) {
							if(!(temp.trim().startsWith("/") || temp.trim().startsWith("*") || temp.equals(""))){
								line++;
							}
						}
						line++;
						method.add(""+line);
						line = 0 ;
						i++;
					}
				} else {
					if(temp.contains("@method:")){
						method.add(temp.split(":")[1].trim());
						while (!(temp = br.readLine()).contains("/")) {
							method.add(temp.split(",")[1].trim());
						}
						flag = true;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}// end
}
